/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.NonexistentEntityException;
import ecfdb.CreditFellah;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ecfdb.Depot;
import ecfdb.Fellah;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author QEMLAB
 */
public class CreditFellahJpaController implements Serializable {

    public CreditFellahJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CreditFellah creditFellah) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Depot idDepot = creditFellah.getIdDepot();
            if (idDepot != null) {
                idDepot = em.getReference(idDepot.getClass(), idDepot.getIddepot());
                creditFellah.setIdDepot(idDepot);
            }
            Fellah idFellah = creditFellah.getIdFellah();
            if (idFellah != null) {
                idFellah = em.getReference(idFellah.getClass(), idFellah.getIdfellah());
                creditFellah.setIdFellah(idFellah);
            }
            em.persist(creditFellah);
            if (idDepot != null) {
                idDepot.getCreditFellahList().add(creditFellah);
                idDepot = em.merge(idDepot);
            }
            if (idFellah != null) {
                idFellah.getCreditFellahList().add(creditFellah);
                idFellah = em.merge(idFellah);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CreditFellah creditFellah) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CreditFellah persistentCreditFellah = em.find(CreditFellah.class, creditFellah.getIdcredit());
            Depot idDepotOld = persistentCreditFellah.getIdDepot();
            Depot idDepotNew = creditFellah.getIdDepot();
            Fellah idFellahOld = persistentCreditFellah.getIdFellah();
            Fellah idFellahNew = creditFellah.getIdFellah();
            if (idDepotNew != null) {
                idDepotNew = em.getReference(idDepotNew.getClass(), idDepotNew.getIddepot());
                creditFellah.setIdDepot(idDepotNew);
            }
            if (idFellahNew != null) {
                idFellahNew = em.getReference(idFellahNew.getClass(), idFellahNew.getIdfellah());
                creditFellah.setIdFellah(idFellahNew);
            }
            creditFellah = em.merge(creditFellah);
            if (idDepotOld != null && !idDepotOld.equals(idDepotNew)) {
                idDepotOld.getCreditFellahList().remove(creditFellah);
                idDepotOld = em.merge(idDepotOld);
            }
            if (idDepotNew != null && !idDepotNew.equals(idDepotOld)) {
                idDepotNew.getCreditFellahList().add(creditFellah);
                idDepotNew = em.merge(idDepotNew);
            }
            if (idFellahOld != null && !idFellahOld.equals(idFellahNew)) {
                idFellahOld.getCreditFellahList().remove(creditFellah);
                idFellahOld = em.merge(idFellahOld);
            }
            if (idFellahNew != null && !idFellahNew.equals(idFellahOld)) {
                idFellahNew.getCreditFellahList().add(creditFellah);
                idFellahNew = em.merge(idFellahNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = creditFellah.getIdcredit();
                if (findCreditFellah(id) == null) {
                    throw new NonexistentEntityException("The creditFellah with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CreditFellah creditFellah;
            try {
                creditFellah = em.getReference(CreditFellah.class, id);
                creditFellah.getIdcredit();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The creditFellah with id " + id + " no longer exists.", enfe);
            }
            Depot idDepot = creditFellah.getIdDepot();
            if (idDepot != null) {
                idDepot.getCreditFellahList().remove(creditFellah);
                idDepot = em.merge(idDepot);
            }
            Fellah idFellah = creditFellah.getIdFellah();
            if (idFellah != null) {
                idFellah.getCreditFellahList().remove(creditFellah);
                idFellah = em.merge(idFellah);
            }
            em.remove(creditFellah);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CreditFellah> findCreditFellahEntities() {
        return findCreditFellahEntities(true, -1, -1);
    }

    public List<CreditFellah> findCreditFellahEntities(int maxResults, int firstResult) {
        return findCreditFellahEntities(false, maxResults, firstResult);
    }

    private List<CreditFellah> findCreditFellahEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CreditFellah.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CreditFellah findCreditFellah(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CreditFellah.class, id);
        } finally {
            em.close();
        }
    }

    public int getCreditFellahCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CreditFellah> rt = cq.from(CreditFellah.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
